/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import java.util.Date;

/**
 *
 * @author Marcio
 */
public class Datas {

    private int day;
    private int month;
    private int year;

    public Datas(int day, int month, int year){
        this.day = day;
        this.month = month;
        this.year = year;

    }

    /**
     * @return the day
     */
    public int getDay() {
        return day;
    }

    /**
     * @param day the day to set
     */
    public void setDay(int day) {
        this.day = day;
        this.DateFix(this.day, this.month);
    }

    /**
     * @return the month
     */
    public int getMonth() {
        return month;
    }

    /**
     * @param month the month to set
     */
    public void setMonth(int month) {
        this.month = month;
    }

    /**
     * @return the year
     */
    public int getYear() {
        return year;
    }

    /**
     * @param year the year to set
     */
    public void setYear(int year) {
        this.year = year;
    }

    // seta o dia para 1 caso o dia digitado pelo usuário seja maior que o número de dias do mês
    private void DateFix(int day, int month){

        switch (month){
            case 1:
                    if(day > 31){
                        day = 1;
                        month++;
                    }
                    break;
            case 2:
                    if(day > 28){
                        day = 1;
                        month++;
                    }
                    break;
            case 3:
                    if(day > 31){
                        day = 1;
                        month++;
                    }
                    break;
            case 4:
                    if(day > 30){
                        day = 1;
                        month++;
                    }
                    break;
            case 5:
                    if(day > 31){
                        day = 1;
                        month++;
                    }
                    break;
            case 6:
                    if(day > 30){
                        day = 1;
                        month++;
                    }
                    break;
            case 7:
                    if(day > 31){
                        day = 1;
                        month++;
                    }
                    break;
            case 8:
                    if(day > 31){
                        day = 1;
                        month++;
                    }
                    break;
            case 9:
                    if(day > 30){
                        day = 1;
                        month++;
                    }
                    break;
            case 10:
                    if(day > 31){
                        day = 1;
                        month++;
                    }
                    break;
            case 11:
                    if(day > 30){
                        day = 1;
                        month++;
                    }
                    break;
            case 12:
                    if(day > 31){
                        day = 1;
                        month = 1;
                    }
                    break;


        }
    }

    public static Datas MySQLDateToJavaDate(String data) {
        Datas d = new Datas(0, 0, 0);
        StringBuilder ano = new StringBuilder();
        StringBuilder mes = new StringBuilder();
        StringBuilder dia = new StringBuilder();


        int i=0;

        do {
            ano.append(data.charAt(i));
            i++;
        } while(data.charAt(i) != '-');
        i++;
//        System.out.println("Ano: " + ano.toString());
        do {
            mes.append(data.charAt(i));
            i++;
        } while(data.charAt(i) != '-');
        i++;
//        System.out.println("Mes: " + mes.toString());
        do {
            dia.append(data.charAt(i));
            i++;
        } while(i < data.length());
//        System.out.println("Dia: " + dia.toString());
        d.setYear(Integer.valueOf(ano.toString()));
        d.setMonth(Integer.valueOf(mes.toString()));
        d.setDay(Integer.valueOf(dia.toString()));

        return d;
    }

    public static String JavaDateToMySQLDate(Datas d) {
        StringBuilder data = new StringBuilder();

        data.append(d.getYear());
        data.append("-");
        data.append(d.getMonth());
        data.append("-");
        data.append(d.getDay());

        return data.toString();
    }

    public static String JavaDateToMySQLDate(String d) {
        StringBuilder data = new StringBuilder();
        StringBuilder dia = new StringBuilder();
        StringBuilder mes = new StringBuilder();
        StringBuilder ano = new StringBuilder();

        int i=0;

        do {
            dia.append(d.charAt(i));
            i++;
        } while(d.charAt(i) != '/');
        i++;
        do {
            mes.append(d.charAt(i));
            i++;
        } while(d.charAt(i) != '/');
        i++;
        do {
            ano.append(d.charAt(i));
            i++;
        } while(i < d.length());

        data.append(ano);
        data.append('-');
        data.append(mes);
        data.append('-');
        data.append(dia);

        return data.toString();
    }

    @Override
    public String toString() {
        return this.day + "/" + this.month + "/" +this.year;
    }

}
